﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Web;

namespace CYWebService
{
    public class GetCYData
    {
        public void GetFile(string fileId,string contentType)
        {

            HttpClient client = new HttpClient();
            //form-data:attachmentId、EngineCode
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            keyValues.Add("attachmentId", fileId);
            keyValues.Add("EngineCode", "yso9orvf4f8ufd1wu9povnsz2");
            //身份认证参数
            FormUrlEncodedContent content = new FormUrlEncodedContent(keyValues);
            content.Headers.Add("EngineCode", "yso9orvf4f8ufd1wu9povnsz2");
            content.Headers.Add("EngineSecret", "1L28RRRuSNivjITdzEekC+Rr4G8A+fxszsh1CHwWG1bzLlBXPBCi5g==");
            //请求执行
            HttpResponseMessage result = client.PostAsync("https://www.h3yun.com/Api/DownloadBizObjectFile", content).Result;
            Stream responseStream = result.Content.ReadAsStreamAsync().Result;
            var header = result.Content.Headers.GetValues("Content-Disposition");//附件名称
            string[] dd = header.ToArray();
            Console.WriteLine(header);
            //创建本地文件
            //bool flag = File.Exists("C:\\MailPic\\" + fileId + contentType);
            //if (flag == false)
            //{
                Stream stream = new FileStream("C:\\MailPic\\" + fileId + contentType, FileMode.Create);
                byte[] bArr = new byte[1024];
                int size = responseStream.Read(bArr, 0, bArr.Length);
                while (size > 0)
                {
                    stream.Write(bArr, 0, size);
                    size = responseStream.Read(bArr, 0, bArr.Length);
                }
                stream.Close();
            
            
                FileStream ddd = new FileStream("C:\\MailPic\\" + fileId + contentType, FileMode.Open);
                var fd = ddd.Length;
                byte[] bytes = new byte[fd];
                ddd.Read(bytes, 0, Convert.ToInt32(fd));
                ddd.Close();
            
            responseStream.Close();

        }
    }
}